package com.smfx.yxt.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.Configuration;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.daoSqlserver.SchoolbusLineMapper;
import com.smfx.yxt.daoSqlserver.SchoolbusLineSiteMapper;
import com.smfx.yxt.daoSqlserver.SchoolbusStudentBoardMapper;
import com.smfx.yxt.daoSqlserver.SchoolbusStudentListMapper;
import com.smfx.yxt.daoSqlserver.StudentInfoMapper;
import com.smfx.yxt.model.SchoolbusLine;
import com.smfx.yxt.model.SchoolbusLineSite;
import com.smfx.yxt.model.SchoolbusStudentBoard;
import com.smfx.yxt.model.SchoolbusStudentList;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.SchoolbusQueryVO;
import com.smfx.yxt.reqVo.SchoolbusSignReqVO;
import com.smfx.yxt.service.ISchoolbusLineService;
import com.smfx.yxt.utils.HttpClientUtils;
import com.smfx.yxt.utils.PropertiesUtil;
import com.smfx.yxt.utils.TextUtil;
import com.smfx.yxt.view.SchoolbusLineView;
import com.smfx.yxt.view.SchoolbusListView;
import com.smfx.yxt.view.SchoolbusSignView;
import com.smfx.yxt.view.TeacherPhoneView;
import com.smfx.yxt.wechat.utils.TemplateUtil;

@Service("schoolbusLineService")
public class SchoolbusLineServiceImpl implements ISchoolbusLineService {

	private Logger logger = LoggerFactory.getLogger(SchoolbusLineServiceImpl.class);
	
	@Autowired
	private SchoolbusLineMapper schoolbusLineMapper;
	@Autowired
	private SchoolbusStudentListMapper schoolbusStudentListMapper;
	@Autowired
	private SchoolbusStudentBoardMapper schoolbusStudentBoardMapper;
	@Autowired
	private SchoolbusLineSiteMapper schoolbusLineSiteMapper;
	@Autowired
	private StudentInfoMapper studentInfoMapper;
	
	@Override
	public int insertSelective(SchoolbusLine record) {
		return schoolbusLineMapper.insertSelective(record);
	}

	@Override
	public SchoolbusLine selectByPrimaryKey(Integer id) {
		return schoolbusLineMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(SchoolbusLine record) {
		return schoolbusLineMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<SchoolbusLine> selectLineByTeacherId(Integer teacherId) {
		return schoolbusLineMapper.selectLineByTeacherId(teacherId);
	}

	@Override
	public List<SchoolbusLine> selectList() {
		return schoolbusLineMapper.selectList();
	}

	@Override
	public SchoolbusLineView selectViewDetailByLineId(Integer lineId) {
		return schoolbusLineMapper.selectViewDetailByLineId(lineId);
	}

	@Override
	public List<SchoolbusListView> selectSchoolbusListView(SchoolbusQueryVO vo) {
		return schoolbusStudentListMapper.selectSchoolbusListView(vo);
	}

	@Override
	public SchoolbusStudentBoard addAndUpdateStudentBoard(SchoolbusStudentBoard record) {
		schoolbusStudentBoardMapper.insertSelective(record);
		try {
			if(record.getIsBoard() == 1) {
				String signUrl = TextUtil.format(Configuration.getInstance().getSchoolbusStudentSign(), 
						new String[]{String.valueOf(new Date().getTime()/1000), 
								String.valueOf(record.getStudentId())});
				String result = HttpClientUtils.doGet(signUrl);
				logger.info("--校车上车签到 result={}", result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return record;
	}

	@Override
	public void addSchoolbusBoardNoticeParent(Integer lineId) {
		List<SchoolbusListView> list = schoolbusStudentListMapper.selectSchoolbusNoticeParent(lineId);
		SchoolbusStudentBoard board = new SchoolbusStudentBoard();
		board.setLineId(lineId);
		schoolbusStudentBoardMapper.updateStartCard(board);
		new Thread(new SchoolbusNoticeExecutor(list)).start();
	}
	
	class SchoolbusNoticeExecutor implements Runnable {
		
		private List<SchoolbusListView> list;
		
		public SchoolbusNoticeExecutor(List<SchoolbusListView> list) {
			this.list = list;
		}

		@Override
		public void run() {
			try { 
				String templateId = PropertiesUtil.getProperty("schoolbus_board_template");
				for(SchoolbusListView view : list) {
					String reqUrl = Const.BASEPATH + Const.STUDENT_ROLE + MenuConst.SCHOOLBUS + "?sid=" + view.getStudentId();
					TemplateUtil.schoolbusStudentBoardSend(
						view.getParentOpenid(), view.getStudentName(), 
						view.getBoardTime(), view.getLineName(), templateId, reqUrl);
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public int isTeacherLine(SchoolbusQueryVO vo) {
		return schoolbusLineMapper.isTeacherLine(vo);
	}

	@Override
	public List<TeacherPhoneView> selectTeacherPhoneForSchoolbus(Integer grade, Integer clazz) {
		return schoolbusLineMapper.selectTeacherPhoneForSchoolbus(grade, clazz);
	}

	@Override
	public List<SchoolbusLine> selectLineForParent(String openid) {
		return schoolbusLineMapper.selectLineForParent(openid);
	}

	@Override
	public List<SchoolbusLineSite> selectSiteListByLineId(Integer lineId) {
		return schoolbusLineSiteMapper.selectSiteListByLineId(lineId);
	}

	@Override
	public RspResult addSchoolbusSign(SchoolbusSignReqVO reqVO) {
		List<SchoolbusSignView> schoolbusSignList = schoolbusStudentListMapper.selectSchoolbusSigner(reqVO.getStudentIds());
		if(schoolbusSignList != null && schoolbusSignList.size() > 0) {
			return new RspResult(Status.CHECK_FAIL, schoolbusSignList);
		}
		List<StudentInfo> list = studentInfoMapper.selectByOpenid(reqVO.getOpenid());
		for(String studentId : reqVO.getStudentIds()) {
			boolean isOk = false;
			for(StudentInfo studentInfo : list) {
				if(studentInfo.getId().toString().equals(studentId)) {
					isOk = true;
					break;
				}
			}
			if(!isOk) {
				return new RspResult(Status.EXPIRED_DATA);
			}
		}
		for(String studentId : reqVO.getStudentIds()) {
			SchoolbusStudentList schoolbusStudentList = new SchoolbusStudentList();
			schoolbusStudentList.setSiteId(reqVO.getSiteId());
			schoolbusStudentList.setStudentId(Integer.valueOf(studentId));
			schoolbusStudentListMapper.insertSelective(schoolbusStudentList);
		}
		return new RspResult(Status.SUCCESS);
	}

	@Override
	public List<SchoolbusListView> selectStudentListByParam(SchoolbusQueryVO vo) {
		return schoolbusStudentListMapper.selectStudentListByParam(vo);
	}

	@Override
	public boolean isStartCar(Integer lineId) {
		return schoolbusStudentBoardMapper.selectCountByLineId(lineId) > 0 ? true : false;
	}

}
